#define MAX_INT 2147483647
#define MIN_INT -2147483648

int reverse(int x)
{
    long res = 0;
    long tmp = x;
    int cache;
    if (x == 0)
    {
        return res;
    }

    /* 问题的关键是需要注意最大边界值，同时符号注意保留*/
    while (tmp != 0)
    {
        cache = tmp % 10;
        res = cache + res * 10;
        tmp /= 10;
    }

    if ((res > MIN_INT) && (res < MAX_INT))
    {
        return res;
    }
    else
    {
        return 0;
    }
}